﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZerAx.ObjectNotation
{
    public class ZonList : Zon
    {
        public override ZonType Type => ZonType.List;

        public readonly List<Zon> data;

        public Zon this[int idx] => data[idx];

        public ZonList()
        {
            data = new();
        }

        public ZonList(IEnumerable<Zon> values)
        {
            data = new(values);
        }

        public ZonList(params Zon[] children) : this(children.AsEnumerable()) { }

        public override void Initialize(BinaryReader reader)
        {
            data.Clear();
            int count = reader.ReadInt32();
            for (int i = 0; i < count; i++)
            {
                data.Add(Deserialze(reader));
            }
        }

        public override void SerializeData(BinaryWriter writer)
        {
            writer.Write(data.Count);
            foreach (var item in data)
            {
                item.Serialize(writer);
            }
        }

        public override string ToString()
        {
            return $"[{string.Join(',', data)}]";
        }
    }
}
